/*
 * Copyright 2007-2009 Thomas Krause (thomas@berlin-reloaded.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gidoo.wickettools;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.Model;

/**
 * This is a page containing a message box very similar to the ones used in desktop-applications
 * @author <a href="mailto:thomas@berlin-reloaded.de">Thomas Krause</a>
 */
public class MessageBox extends WebPage
{
  
  public enum MessageBoxResult {YES, NO, ABORT};
  
  public enum MessageBoxButtons {OK, YES_NO, YES_NO_ABORT};
  
  private MessageBoxResult _result;
  private Page _returnPage;
  private IMessageBoxResultReceiver _receiver;
  
  /**
   * Simple constructor
   * @param text The message text displayed to the user. May not contain (X)HTML.
   * @param returnPage The page where to return to after the user made is choice.
   * @param receiver The receiver of the result of the users action.
   */
  public MessageBox(String text, Page returnPage, IMessageBoxResultReceiver receiver)
  {
    this(null, text, false, MessageBoxButtons.OK, returnPage, receiver);
  }
  
  /**
   * Complex constructor
   * @param title The title of the web page which is displayed to the user.
   * @param text The message text displayed to the user. May contain (X)HTML if
   * specified by the next parameter.
   * @param textIsHTML Wether the text should be escaped or not. If true it is not escaped.
   * @param buttons The choices that are displayed to the user (Ok, Yes, No, Abort, ...)
   * @param returnPage The page where to return to after the user made is choice.
   * @param receiver The receiver of the result of the users action.
   */
  public MessageBox(String title, String text,
    boolean textIsHTML,MessageBoxButtons buttons, Page returnPage,
    IMessageBoxResultReceiver receiver)
  {
    _result = MessageBoxResult.NO;
    _returnPage = returnPage;
    _receiver = receiver;
    
    if(title == null)
      title = getString("title");
    
    add(new Label("title", new Model(title)));
    
    Label lblText = new Label("text", new Model(text));
    lblText.setEscapeModelStrings(!textIsHTML);
    add(lblText);
    
    
    if(buttons == MessageBoxButtons.OK)
    {
      Fragment frag = new Fragment("buttons", "fragOk", this);
      
      Link lnkOk = new Link("lnkOk")
      {
        public void onClick()
        {
          _result = MessageBoxResult.YES;
          
          goAhead();
        }
      };
      
      lnkOk.add(new Label("lblOk", new Model(getString("lblOk"))));
      frag.add(lnkOk);
      add(frag);
    }
    else if(buttons == MessageBoxButtons.YES_NO || buttons == MessageBoxButtons.YES_NO_ABORT)
    {
      Fragment frag = new Fragment("buttons", "fragYesNoAbort", this);
      
      Link lnkYes = new Link("lnkYes")
      {
        public void onClick()
        {
          _result = MessageBoxResult.YES;
          goAhead();
        }
      };
      
      lnkYes.add(new Label("lblYes", new Model(getString("lblYes"))));
      frag.add(lnkYes);
      
      Link lnkNo = new Link("lnkNo")
      {
        public void onClick()
        {
          _result = MessageBoxResult.NO;
          goAhead();
        }
      };
      
      lnkNo.add(new Label("lblNo", new Model(getString("lblNo"))));
      frag.add(lnkNo);

      Link lnkAbort = new Link("lnkAbort")
      {
        public void onClick()
        {
          _result = MessageBoxResult.ABORT;
          goAhead();
        }
      };
      
      lnkAbort.add(new Label("lblAbort", new Model(getString("lblAbort"))));
      frag.add(lnkAbort);
      
      if(buttons == MessageBoxButtons.YES_NO_ABORT)
      {
        lnkAbort.setVisible(true);
      }
      else
      {
        lnkAbort.setVisible(false);
      }
      
      add(frag);
    }
  }
  
  private void goAhead()
  {
    if(_receiver != null)
    {
      _receiver.receiveMessageBoxResult(_result);
    }
    setResponsePage(_returnPage);
  }
  
  /**
   * Get the result. Depends on the button the user has clicked.
   * @return The result.
   */
  public MessageBoxResult getResult()
  {
    return _result;
  }

  
}
